//
// Created on 2022/11/11.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include <js_native_api_types.h>
#include <node_api_types.h>
#include <memory>
#include <string>
#include <mutex>
#include <thread>

#ifndef NAPI_learning_WorkDemo_H
#define NAPI_learning_WorkDemo_H

struct SyncParam{
    double num1 = 0;
    double num2 = 0;
    double result = 0;
    
    napi_async_work worker = nullptr;
    napi_deferred deferred = nullptr;
};

struct ThreadInfo{
    std::mutex mutex;
    std::condition_variable condition;
    bool ready = false;
};

struct WorkParam{
    napi_env env;
    //in param for work function
    std::string inParam;
    //out result for work
    char *outParam;
    std::shared_ptr<ThreadInfo> lockInfo;
    napi_value callback;
    napi_value context;
};

//演示同步，异步方法调用
class WorkDemo{
    public:
        //同步
        static napi_value Add(napi_env env, napi_callback_info info);
        static napi_value Sub(napi_env env, napi_callback_info info);
        static napi_value hyPotC(napi_env env, napi_callback_info info);
        static napi_value addSync(napi_env env, napi_callback_info info);
        //异步
        static napi_value addAsyncByPromise(napi_env env, napi_callback_info info);
        static napi_value getAsyncByCallback(napi_env env, napi_callback_info info);

        //调用系统类Display的方法
        static napi_value CallPromiseMethod(napi_env env, napi_callback_info info);

        //目前看是JS  -》  C++  -》  JS
        static napi_value CallPromiseMethodSync(napi_env env, napi_callback_info info);
        static void RunPromiseBySync(WorkParam *param) ;
        static void RunCallbackMethod(WorkParam *param) ;
};

struct SyncParamAsync {
    double num1 = 0;
    double num2 = 0;
    double result = 0;
    napi_async_work work = nullptr;
    napi_deferred deferred = nullptr;
    
    napi_ref callbackRef = nullptr;
    napi_value thisVar = nullptr;
};




#endif //NAPI_learning_hello.h_H
